﻿using Microsoft.SemanticKernel;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace Mozhi.Agent.DeepSeek.Plugins
{
    public class PurchaseOrderPlugin
    {
        [KernelFunction("create_purchase_order")]
        [Description("create purchase order")]
        public async Task<PuchaseOrder> CreatePurchaseOrder(string result)
        {
            var purchaseOrder = JsonConvert.DeserializeObject<PuchaseOrder>(result);

            if (purchaseOrder.Items.Count == 0)
                throw new Exception("item is required");

            if(string.IsNullOrEmpty(purchaseOrder.Supplier) && !purchaseOrder.SupplierId.HasValue)
                throw new Exception("supplier is required");

            if(!string.IsNullOrEmpty(purchaseOrder.Supplier) && !purchaseOrder.SupplierId.HasValue)
                purchaseOrder.SupplierId = (await new SupplierPlugin().GetSuppliersAsync()).FirstOrDefault(x=>x.Name == purchaseOrder.Supplier)?.Id;

            foreach(var item in purchaseOrder.Items)
            {
                if (string.IsNullOrEmpty(item.Product) && !item.ProductId.HasValue)
                    throw new Exception("productId is required");

                if(item.Price<=0)
                    throw new Exception("item price is required");

                if (!string.IsNullOrEmpty(item.Product) && !item.ProductId.HasValue)
                    item.ProductId = (await new ProductPlugin().GetProductsAsync()).FirstOrDefault(x => x.Name == item.Product)?.Id;

                if(item.Quantity<=0)
                    throw new Exception("item quantity is required");

                item.Amount = item.Amount == 0 ? item.Price * item.Quantity : item.Amount;

                item.Id = item.Id ?? Guid.NewGuid();
            }

            purchaseOrder.Number = "PO-" + DateTime.Now.ToString("yyyyMMddHHmmss");

            purchaseOrder.Id = purchaseOrder.Id ?? Guid.NewGuid();

            return purchaseOrder;
        }
    }

    public class PuchaseOrder
    {
        [JsonPropertyName("id")]
        public Guid? Id { get; set; }

        [JsonPropertyName("number")]
        public string Number { get; set; }

        [JsonPropertyName("date")]
        public DateTime Date { get; set; }

        [JsonPropertyName("supplierId")]
        public Guid? SupplierId { get; set; }

        [JsonPropertyName("supplier")]
        public string Supplier { get; set; }

        [JsonPropertyName("items")]
        public List<PurchaseOrderItem> Items { get; set; }
    }

    public class PurchaseOrderItem
    {
        [JsonPropertyName("id")]
        public Guid? Id { get; set; }

        [JsonPropertyName("productId")]
        public Guid? ProductId { get; set; }

        [JsonPropertyName("product")]
        public string Product {  get; set; }

        [JsonPropertyName("price")]
        public decimal Price { get; set; }

        [JsonPropertyName("quantity")]
        public decimal Quantity { get; set; }

        [JsonPropertyName("amount")]
        public decimal Amount { get; set; }
    }
}
